package com.open.capacity.finance.controller;

import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.RectangleReadOnly;
import com.itextpdf.text.pdf.PdfWriter;
import com.open.capacity.commons.Result;
import com.open.capacity.finance.dto.*;
import com.open.capacity.finance.service.ReportService;
import com.open.capacity.finance.util.*;
import com.open.capacity.reptile.service.GdszczMaEleAccoService;
import com.open.capacity.user.service.SysDictDataService;
import com.open.capacity.utils.StringUtil;
import com.open.capacity.utils.file.FileUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 报表
 *
 * @author
 * @email
 * @date 2020-06-05 17:40:41
 */
@RestController
@RequestMapping("reports")
@Api(tags = "报表")
@Slf4j
public class PzReportController {

    @Autowired
    private ReportService reportService;

    @Autowired
    private GdszczMaEleAccoService kjkmService;

    @Autowired
    private SysDictDataService dictService;

    @Value("${file.uploadpath:/usr/local/files/upload}")
    private String fileuploadPath;

    //加工过滤参数
    private Map<String, Object> filterParam(Map<String, Object> params) {
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());

        if (params.containsKey("kjkmCodes")) {
            String kjkmCodes = params.get("kjkmCodes").toString();
            String[] kjkmArr = kjkmCodes.split(",");
            if (StringUtil.isEmpty(kjkmCodes.trim())) {
                params.remove("kjkmCodes");
            } else {
                String[] kjkmExtArr = kjkmService.findChildCodes(companyId, ztId, kjnd, kjkmArr);
                if (kjkmExtArr.length > 0) {
                    params.put("kjkmCodes", kjkmExtArr);
                    params.put("kjkmCodes_select", kjkmArr);
                }
            }
        }


        boolean expecoEnable = params.containsKey("expecoEnable") && params.get("expecoEnable").toString().equals("1");
        boolean expfuncEnable = params.containsKey("expfuncEnable") && params.get("expfuncEnable").toString().equals("1");
        boolean projectEnable = params.containsKey("projectEnable") && params.get("projectEnable").toString().equals("1");
        boolean depproEnable = params.containsKey("depproEnable") && params.get("depproEnable").toString().equals("1");
        boolean departmentEnable = params.containsKey("departmentEnable") && params.get("departmentEnable").toString().equals("1");


        if (params.containsKey("projectCodes")) {
            String projectCodes = params.get("projectCodes").toString();
            if (StringUtil.isEmpty(projectCodes.trim())) {
                params.remove("projectCodes");
            } else {
                String[] projectArr = projectCodes.split(",");
                params.put("projectCodes", projectArr);
            }
        }
        if (params.containsKey("depproCodes")) {
            String depproCodes = params.get("depproCodes").toString();
            if (StringUtil.isEmpty(depproCodes.trim())) {
                params.remove("depproCodes");
            } else {
                String[] depproArr = depproCodes.split(",");
                params.put("depproCodes", depproArr);
            }
        }
        if (params.containsKey("departmentCodes")) {
            String departmentCodes = params.get("departmentCodes").toString();
            if (StringUtil.isEmpty(departmentCodes.trim())) {
                params.remove("depproCodes");
            } else {
                String[] departmentArr = departmentCodes.split(",");
                params.put("departmentCodes", departmentArr);
            }
        }
        boolean expecoSelect = false;
        if (params.containsKey("expecoCodes")) {
            String expecoCodes = params.get("expecoCodes").toString();
            if (StringUtil.isEmpty(expecoCodes.trim())) {
                params.remove("expecoCodes");
            } else {
                String[] expecoArr = expecoCodes.split(",");
                params.put("expecoCodes", expecoArr);
                expecoSelect = true;
            }
        }
        boolean expfuncSelect = false;
        if (params.containsKey("expfuncCodes")) {
            String expfuncCodes = params.get("expfuncCodes").toString();
            if (StringUtil.isEmpty(expfuncCodes.trim())) {
                params.remove("expfuncCodes");
            } else {
                String[] pjhArr = expfuncCodes.split(",");
                params.put("expfuncCodes", pjhArr);
                expfuncSelect = true;
            }
        }

        if (!params.containsKey("expecoCodes") && !expecoSelect && params.containsKey("expecoCodes_input")) {
            String expecoCodes_input = params.get("expecoCodes_input").toString();
            if (StringUtil.isNotEmpty(expecoCodes_input.trim())) {
                params.remove("expecoCodes");
                params.put("expecoCodes", expecoCodes_input);
            }
        }
        if (!params.containsKey("expfuncCodes") && !expfuncSelect && params.containsKey("expfuncCodes_input")) {
            String expfuncCodes_input = params.get("expfuncCodes_input").toString();
            if (StringUtil.isNotEmpty(expfuncCodes_input.trim())) {
                params.remove("expfuncCodes");
                params.put("expfuncCodes", expfuncCodes_input);
            }
        }
        return params;
    }

    /**
     * 科目余额表
     */
    @RequestMapping("/kmyeb")
    public Result<List<KmyebDto>> kmyeb(@RequestParam Map<String, Object> params) {
        params = filterParam(params);
        List<KmyebDto> data = reportService.kmyeb(params);
        return Result.succeed(data, "成功");
    }

    /**
     * 科目余额表(导出excel)
     *
     * @return
     */
    @GetMapping("/kmyeb/excel")
    public void exportKmyebExcel(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<KmyebDto> data = new ArrayList<>();
        params = filterParam(params);

        String ztId = params.get("ztId").toString();
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        try {
            data = reportService.kmyeb(params);
            for (KmyebDto item : data) {
                if (item.ncye == null) {
                    item.ncye = BigDecimal.ZERO;
                }
                if (item.qcye == null) {
                    item.qcye = BigDecimal.ZERO;
                }
                if (item.jfs == null) {
                    item.jfs = BigDecimal.ZERO;
                }
                if (item.dfs == null) {
                    item.dfs = BigDecimal.ZERO;
                }
                if (item.jlj == null) {
                    item.jlj = BigDecimal.ZERO;
                }
                if (item.dlj == null) {
                    item.dlj = BigDecimal.ZERO;
                }
                if (item.ye == null) {
                    item.ye = BigDecimal.ZERO;
                }
            }
        } catch (Exception e) {
            log.error("/kmyeb/bill失败{}", e);
        }

        ExcelKmyeb excel = new ExcelKmyeb();

        String[] headNames = {"编号", "名称", "借/贷", "年初余额", "借/贷", "期初余额", "借发生", "贷发生", "借累计", "贷累计", "借/贷", "期末余额"};
        int colWidths[] = {100, 150, 60, 150, 60, 150, 150, 150, 150, 150, 60, 150};

        InputStream input = (excel.getExcelFile(ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, "科目余额表", headNames, colWidths));

        String filePath = fileuploadPath + "/temp/excel/kmyeb.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "kmyeb.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目余额表(导出pdf)
     *
     * @return
     */
    @GetMapping("/kmyeb/pdf")
    public void exportKmyebPdf(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<KmyebDto> data = new ArrayList<>();
        params = filterParam(params);

        String ztId = params.get("ztId").toString();
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        try {
            data = reportService.kmyeb(params);
            for (KmyebDto item : data) {
                if (item.ncye == null) {
                    item.ncye = BigDecimal.ZERO;
                }
                if (item.qcye == null) {
                    item.qcye = BigDecimal.ZERO;
                }
                if (item.jfs == null) {
                    item.jfs = BigDecimal.ZERO;
                }
                if (item.dfs == null) {
                    item.dfs = BigDecimal.ZERO;
                }
                if (item.jlj == null) {
                    item.jlj = BigDecimal.ZERO;
                }
                if (item.dlj == null) {
                    item.dlj = BigDecimal.ZERO;
                }
                if (item.ye == null) {
                    item.ye = BigDecimal.ZERO;
                }
            }
        } catch (Exception e) {
            log.error("/kmyeb失败{}", e);
        }

        String[] headNames = {"编号", "名称", "借/贷", "年初余额", "借/贷", "期初余额", "借发生", "贷发生", "借累计", "贷累计", "借/贷", "期末余额"};
        float colWidths[] = {100, 150, 60, 150, 60, 150, 150, 150, 150, 150, 60, 150};

        String filePath = fileuploadPath + "/temp/pdf/kmyeb.pdf";
        Document document = new Document(new RectangleReadOnly(842F, 595F));
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("科目余额表");// 标题
            new PdfKmyeb().generatePDF(document, ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, headNames, colWidths);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "kmyeb.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }


    /**
     * 辅助帐余额表
     */
    @RequestMapping("/fzxyeb")
    public Result<List<FzxyebDto>> fzxyeb(@RequestParam Map<String, Object> params) {
        params = filterParam(params);
        boolean kjkmEnable = params.containsKey("kjkmEnable") && params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");

        int kjkmXh = StringUtil.toInt(params.get("kjkmXh").toString());
        int ajXh = StringUtil.toInt(params.get("ajXh").toString());
        int pjhXh = StringUtil.toInt(params.get("pjhXh").toString());
        int zphXh = StringUtil.toInt(params.get("zphXh").toString());
        int wldxXh = StringUtil.toInt(params.get("wldxXh").toString());
        int xmXh = StringUtil.toInt(params.get("xmXh").toString());
        int bmXh = StringUtil.toInt(params.get("bmXh").toString());

        int count = 0;
        TreeSet<Integer> xhSet = new TreeSet<>();
        if (kjkmEnable) {
            xhSet.add(kjkmXh);
            count++;
        }
        if (ajEnable) {
            xhSet.add(ajXh);
            count++;
        }
        if (pjhEnable) {
            xhSet.add(pjhXh);
            count++;
        }
        if (zphEnable) {
            xhSet.add(zphXh);
            count++;
        }
        if (wldxEnable) {
            xhSet.add(wldxXh);
            count++;
        }
        if (xmEnable) {
            xhSet.add(xmXh);
            count++;
        }
        if (bmEnable) {
            xhSet.add(bmXh);
            count++;
        }
        Assert.isTrue(xhSet.size() == count, "序号请严格按照顺序来排列，例如1，2，3，4，5，6...");

        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        List<FzxyebDto> data = reportService.fzxyeb(params);
        return Result.succeed(data, "成功");
    }


    /**
     * 辅助帐余额表(导出excel)
     */
    @GetMapping("/fzxyeb/excel")
    public void exportFzxyebExcel(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        params = filterParam(params);
        String ztId = params.get("ztId").toString();
        boolean kjkmEnable = params.containsKey("kjkmEnable") && params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");

        int kjkmXh = StringUtil.toInt(params.get("kjkmXh").toString());
        int ajXh = StringUtil.toInt(params.get("ajXh").toString());
        int pjhXh = StringUtil.toInt(params.get("pjhXh").toString());
        int zphXh = StringUtil.toInt(params.get("zphXh").toString());
        int wldxXh = StringUtil.toInt(params.get("wldxXh").toString());
        int xmXh = StringUtil.toInt(params.get("xmXh").toString());
        int bmXh = StringUtil.toInt(params.get("bmXh").toString());

        int count = 0;
        TreeSet<Integer> xhSet = new TreeSet<>();
        if (kjkmEnable) {
            xhSet.add(kjkmXh);
            count++;
        }
        if (ajEnable) {
            xhSet.add(ajXh);
            count++;
        }
        if (pjhEnable) {
            xhSet.add(pjhXh);
            count++;
        }
        if (zphEnable) {
            xhSet.add(zphXh);
            count++;
        }
        if (wldxEnable) {
            xhSet.add(wldxXh);
            count++;
        }
        if (xmEnable) {
            xhSet.add(xmXh);
            count++;
        }
        if (bmEnable) {
            xhSet.add(bmXh);
            count++;
        }
        Assert.isTrue(xhSet.size() == count, "序号请严格按照顺序来排列，例如1，2，3，4，5，6...");

        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        List<FzxyebDto> data = new ArrayList<>();
        try {
            data = reportService.fzxyeb(params);
        } catch (Exception e) {
            log.error("/fzxyeb:{}", e);
        }
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        TreeMap<Integer, String> hsColMap = new TreeMap<>();

        if (kjkmEnable) {
            hsColMap.put(kjkmXh, "科目");
        }
        if (ajEnable) {
            hsColMap.put(ajXh, "案件");
        }
        if (pjhEnable) {
            hsColMap.put(pjhXh, "票据号");
        }
        if (zphEnable) {
            hsColMap.put(zphXh, "支票号");
        }
        if (wldxEnable) {
            hsColMap.put(wldxXh, "往来对象");
        }
        if (bmEnable) {
            hsColMap.put(bmXh, "部门");
        }
        if (xmEnable) {
            hsColMap.put(xmXh, "项目");
        }
        List<String> colDatas = new ArrayList<>();
        //保证有序
        for (Map.Entry<Integer, String> m : hsColMap.entrySet()) {
            String val = m.getValue();
            colDatas.add(val);
        }


        ExcelFzxyeb excel = new ExcelFzxyeb();
        String[] headNames = new String[colDatas.size() + 10];
        int colWidths[] = new int[colDatas.size() + 10];
        for (int i = 0; i < colDatas.size(); i++) {
            headNames[i] = colDatas.get(i);
            colWidths[i] = 250;
        }
        headNames[colDatas.size()] = "借/贷";
        headNames[colDatas.size() + 1] = "年初余额";
        headNames[colDatas.size() + 2] = "借/贷";
        headNames[colDatas.size() + 3] = "期初余额";
        headNames[colDatas.size() + 4] = "借发生";
        headNames[colDatas.size() + 5] = "贷发生";
        headNames[colDatas.size() + 6] = "借累计";
        headNames[colDatas.size() + 7] = "贷累计";
        headNames[colDatas.size() + 8] = "借/贷";
        headNames[colDatas.size() + 9] = "余额";

        colWidths[colDatas.size()] = 75;
        colWidths[colDatas.size() + 1] = 150;
        colWidths[colDatas.size() + 2] = 75;
        colWidths[colDatas.size() + 3] = 150;
        colWidths[colDatas.size() + 4] = 150;
        colWidths[colDatas.size() + 5] = 150;
        colWidths[colDatas.size() + 6] = 150;
        colWidths[colDatas.size() + 7] = 150;
        colWidths[colDatas.size() + 8] = 75;
        colWidths[colDatas.size() + 9] = 150;

        InputStream input = (excel.getExcelFile(ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, "辅助项余额表", headNames, colWidths, params));

        String filePath = fileuploadPath + "/temp/excel/fzxyeb.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "fzxyeb.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 辅助帐余额表(导出pdf)
     */
    @GetMapping("/fzxyeb/pdf")
    public void exportFzxyebPdf(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        params = filterParam(params);
        String ztId = params.get("ztId").toString();
        boolean kjkmEnable = params.containsKey("kjkmEnable") && params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");

        int kjkmXh = StringUtil.toInt(params.get("kjkmXh").toString());
        int ajXh = StringUtil.toInt(params.get("ajXh").toString());
        int pjhXh = StringUtil.toInt(params.get("pjhXh").toString());
        int zphXh = StringUtil.toInt(params.get("zphXh").toString());
        int wldxXh = StringUtil.toInt(params.get("wldxXh").toString());
        int xmXh = StringUtil.toInt(params.get("xmXh").toString());
        int bmXh = StringUtil.toInt(params.get("bmXh").toString());

        int count = 0;
        TreeSet<Integer> xhSet = new TreeSet<>();
        if (kjkmEnable) {
            xhSet.add(kjkmXh);
            count++;
        }
        if (ajEnable) {
            xhSet.add(ajXh);
            count++;
        }
        if (pjhEnable) {
            xhSet.add(pjhXh);
            count++;
        }
        if (zphEnable) {
            xhSet.add(zphXh);
            count++;
        }
        if (wldxEnable) {
            xhSet.add(wldxXh);
            count++;
        }
        if (xmEnable) {
            xhSet.add(xmXh);
            count++;
        }
        if (bmEnable) {
            xhSet.add(bmXh);
            count++;
        }
        Assert.isTrue(xhSet.size() == count, "序号请严格按照顺序来排列，例如1，2，3，4，5，6...");

        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        List<FzxyebDto> data = new ArrayList<>();
        try {
            data = reportService.fzxyeb(params);
        } catch (Exception e) {
            log.error("/fzxyeb:{}", e);
        }
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        TreeMap<Integer, String> hsColMap = new TreeMap<>();

        if (kjkmEnable) {
            hsColMap.put(kjkmXh, "科目");
        }
        if (ajEnable) {
            hsColMap.put(ajXh, "案件");
        }
        if (pjhEnable) {
            hsColMap.put(pjhXh, "票据号");
        }
        if (zphEnable) {
            hsColMap.put(zphXh, "支票号");
        }
        if (wldxEnable) {
            hsColMap.put(wldxXh, "往来对象");
        }
        if (bmEnable) {
            hsColMap.put(bmXh, "部门");
        }
        if (xmEnable) {
            hsColMap.put(xmXh, "项目");
        }
        List<String> colDatas = new ArrayList<>();
        //保证有序
        for (Map.Entry<Integer, String> m : hsColMap.entrySet()) {
            String val = m.getValue();
            colDatas.add(val);
        }


        String[] headNames = new String[colDatas.size() + 10];
        float colWidths[] = new float[colDatas.size() + 10];
        for (int i = 0; i < colDatas.size(); i++) {
            headNames[i] = colDatas.get(i);
            colWidths[i] = 250;
        }
        headNames[colDatas.size()] = "借/贷";
        headNames[colDatas.size() + 1] = "年初余额";
        headNames[colDatas.size() + 2] = "借/贷";
        headNames[colDatas.size() + 3] = "期初余额";
        headNames[colDatas.size() + 4] = "借发生";
        headNames[colDatas.size() + 5] = "贷发生";
        headNames[colDatas.size() + 6] = "借累计";
        headNames[colDatas.size() + 7] = "贷累计";
        headNames[colDatas.size() + 8] = "借/贷";
        headNames[colDatas.size() + 9] = "余额";

        colWidths[colDatas.size()] = 75;
        colWidths[colDatas.size() + 1] = 150;
        colWidths[colDatas.size() + 2] = 75;
        colWidths[colDatas.size() + 3] = 150;
        colWidths[colDatas.size() + 4] = 150;
        colWidths[colDatas.size() + 5] = 150;
        colWidths[colDatas.size() + 6] = 150;
        colWidths[colDatas.size() + 7] = 150;
        colWidths[colDatas.size() + 8] = 75;
        colWidths[colDatas.size() + 9] = 150;

        String filePath = fileuploadPath + "/temp/pdf/fzxyeb.pdf";
        Document document = new Document(new RectangleReadOnly(842F, 595F));
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("辅助项余额表");// 标题
            new PdfFzxyeb().generatePDF(document, ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, headNames, colWidths, params);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "fzxyeb.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }


    /**
     * 辅助帐余额表表头
     */
    @RequestMapping("/fzxyeb_header2")
    public Result<GridHeadDataDto2> fzxyebHeader2(@RequestParam Map<String, Object> params) {
        boolean kjkmEnable = params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.get("bmEnable").toString().equals("1");
        TreeMap<Integer, GridHeadDataDto2.ColData> hsColMap = new TreeMap<>();
        TreeMap<Integer, GridHeadDataDto2.ColData2> hsColMap2 = new TreeMap<>();

        int kjkmXh = StringUtil.toInt(params.get("kjkmXh").toString());
        int ajXh = StringUtil.toInt(params.get("ajXh").toString());
        int pjhXh = StringUtil.toInt(params.get("pjhXh").toString());
        int zphXh = StringUtil.toInt(params.get("zphXh").toString());
        int wldxXh = StringUtil.toInt(params.get("wldxXh").toString());
        int xmXh = StringUtil.toInt(params.get("xmXh").toString());
        int bmXh = StringUtil.toInt(params.get("bmXh").toString());

        GridHeadDataDto2 data = new GridHeadDataDto2();

        List<GridHeadDataDto2.ColData> colDatas = new ArrayList<>();
        List<GridHeadDataDto2.ColData2> colDatas2 = new ArrayList<>();

        GridHeadDataDto2.ColData km = new GridHeadDataDto2.ColData.Builder().label("科目").name("col_kjkm").align("left").width(15).build();
        GridHeadDataDto2.ColData aj = new GridHeadDataDto2.ColData.Builder().label("案件").name("col_aj").align("left").width(15).build();
        GridHeadDataDto2.ColData pjh = new GridHeadDataDto2.ColData.Builder().label("票据号").name("col_pjh").align("left").width(15).build();
        GridHeadDataDto2.ColData zph = new GridHeadDataDto2.ColData.Builder().label("支票号").name("col_zph").align("left").width(15).build();
        GridHeadDataDto2.ColData wldx = new GridHeadDataDto2.ColData.Builder().label("往来对象").name("col_wldx").align("left").width(15).build();
        GridHeadDataDto2.ColData xm = new GridHeadDataDto2.ColData.Builder().label("项目").name("col_xm").align("left").width(15).build();
        GridHeadDataDto2.ColData bm = new GridHeadDataDto2.ColData.Builder().label("部门").name("col_bm").align("left").width(15).build();

        GridHeadDataDto2.ColData yefx1 = new GridHeadDataDto2.ColData.Builder().label("借/贷").name("col_yefx1Str").align("center").width(3).build();
        GridHeadDataDto2.ColData ncye = new GridHeadDataDto2.ColData.Builder().label("年初余额").name("col_ncye").align("right").width(10).build();
        GridHeadDataDto2.ColData yefx2 = new GridHeadDataDto2.ColData.Builder().label("借/贷").name("col_yefx2Str").align("center").width(3).build();
        GridHeadDataDto2.ColData qcye = new GridHeadDataDto2.ColData.Builder().label("期初余额").name("col_qcye").align("right").width(10).build();


        GridHeadDataDto2.ColData jfs = new GridHeadDataDto2.ColData.Builder().label("借发生").name("col_jfs").align("right").width(10).build();
        GridHeadDataDto2.ColData dfs = new GridHeadDataDto2.ColData.Builder().label("贷发生").name("col_dfs").align("right").width(10).build();

        GridHeadDataDto2.ColData jlj = new GridHeadDataDto2.ColData.Builder().label("借累计").name("col_jlj").align("right").width(10).build();
        GridHeadDataDto2.ColData dlj = new GridHeadDataDto2.ColData.Builder().label("贷累计").name("col_dlj").align("right").width(10).build();

        GridHeadDataDto2.ColData yefx3 = new GridHeadDataDto2.ColData.Builder().label("借/贷").name("col_yefx3Str").align("center").width(3).build();
        GridHeadDataDto2.ColData ye = new GridHeadDataDto2.ColData.Builder().label("期末余额").name("col_ye").align("right").width(10).build();

        //GridHeadDataDto2.ColData id = new GridHeadDataDto2.ColData.Builder().label("id").name("id").align("center").width(1).build();

        GridHeadDataDto2.ColData2 km2 = new GridHeadDataDto2.ColData2.Builder().title("会计科目").field("kjkm").colspan(1).rowspan(1).checked(true).columnId("kjkm").width(84).align("left").build();
        GridHeadDataDto2.ColData2 aj2 = new GridHeadDataDto2.ColData2.Builder().title("案件").field("aj").colspan(1).rowspan(1).checked(true).columnId("aj").width(84).align("left").build();
        GridHeadDataDto2.ColData2 pjh2 = new GridHeadDataDto2.ColData2.Builder().title("票据号").field("pjh").colspan(1).rowspan(1).checked(true).columnId("pjh").width(84).align("left").build();
        GridHeadDataDto2.ColData2 zph2 = new GridHeadDataDto2.ColData2.Builder().title("支票号").field("zph").colspan(1).rowspan(1).checked(true).columnId("zph").width(84).align("left").build();
        GridHeadDataDto2.ColData2 wldx2 = new GridHeadDataDto2.ColData2.Builder().title("往来对象").field("wldx").colspan(1).rowspan(1).checked(true).columnId("wldx").width(84).align("left").build();
        GridHeadDataDto2.ColData2 xm2 = new GridHeadDataDto2.ColData2.Builder().title("项目").field("xm").colspan(1).rowspan(1).checked(true).columnId("xm").width(84).align("left").build();
        GridHeadDataDto2.ColData2 bm2 = new GridHeadDataDto2.ColData2.Builder().title("部门").field("bm").colspan(1).rowspan(1).checked(true).columnId("bm").width(84).align("left").build();

        GridHeadDataDto2.ColData2 yefx21 = new GridHeadDataDto2.ColData2.Builder().title("余额方向").field("yefx1Str").colspan(1).rowspan(1).checked(true).columnId("yefx1Str").width(25).align("center").build();
        GridHeadDataDto2.ColData2 ncye2 = new GridHeadDataDto2.ColData2.Builder().title("年初余额").field("ncye").colspan(1).rowspan(1).checked(true).columnId("ncye").width(46).align("right").build();
        GridHeadDataDto2.ColData2 yefx22 = new GridHeadDataDto2.ColData2.Builder().title("余额方向").field("yefx2Str").colspan(1).rowspan(1).checked(true).columnId("yefx2Str").width(25).align("center").build();
        GridHeadDataDto2.ColData2 qcye2 = new GridHeadDataDto2.ColData2.Builder().title("期初余额").field("qcye").colspan(1).rowspan(1).checked(true).columnId("qcye").width(46).align("right").build();
        GridHeadDataDto2.ColData2 jfs2 = new GridHeadDataDto2.ColData2.Builder().title("借发生").field("jfs").colspan(1).rowspan(1).checked(true).columnId("jfs").width(46).align("right").build();
        GridHeadDataDto2.ColData2 dfs2 = new GridHeadDataDto2.ColData2.Builder().title("贷发生").field("dfs").colspan(1).rowspan(1).checked(true).columnId("dfs").width(46).align("right").build();
        GridHeadDataDto2.ColData2 jlj2 = new GridHeadDataDto2.ColData2.Builder().title("借累计").field("jlj").colspan(1).rowspan(1).checked(true).columnId("jlj").width(46).align("right").build();
        GridHeadDataDto2.ColData2 dlj2 = new GridHeadDataDto2.ColData2.Builder().title("贷累计").field("dlj").colspan(1).rowspan(1).checked(true).columnId("dlj").width(46).align("right").build();
        GridHeadDataDto2.ColData2 yefx23 = new GridHeadDataDto2.ColData2.Builder().title("余额方向").field("yefx3Str").colspan(1).rowspan(1).checked(true).columnId("yefx3Str").width(25).align("center").build();
        GridHeadDataDto2.ColData2 ye2 = new GridHeadDataDto2.ColData2.Builder().title("余额").field("ye").colspan(1).rowspan(1).checked(true).columnId("ye").width(46).align("right").build();

        if (kjkmEnable) {
            hsColMap.put(kjkmXh, km);
            hsColMap2.put(kjkmXh, km2);
        }
        if (ajEnable) {
            hsColMap.put(ajXh, aj);
            hsColMap2.put(ajXh, aj2);
        }
        if (pjhEnable) {
            hsColMap.put(pjhXh, pjh);
            hsColMap2.put(pjhXh, pjh2);
        }
        if (zphEnable) {
            hsColMap.put(zphXh, zph);
            hsColMap2.put(zphXh, zph2);
        }
        if (wldxEnable) {
            hsColMap.put(wldxXh, wldx);
            hsColMap2.put(wldxXh, wldx2);
        }
        if (bmEnable) {
            hsColMap.put(bmXh, bm);
            hsColMap2.put(bmXh, bm2);
        }
        if (xmEnable) {
            hsColMap.put(xmXh, xm);
            hsColMap2.put(xmXh, xm2);
        }
        //保证有序
        for (Map.Entry<Integer, GridHeadDataDto2.ColData> m : hsColMap.entrySet()) {
            GridHeadDataDto2.ColData val = m.getValue();
            colDatas.add(val);
        }
        for (Map.Entry<Integer, GridHeadDataDto2.ColData2> m : hsColMap2.entrySet()) {
            GridHeadDataDto2.ColData2 val = m.getValue();
            colDatas2.add(val);
        }

        colDatas.add(yefx1);
        colDatas.add(ncye);
        colDatas.add(yefx2);
        colDatas.add(qcye);

        colDatas.add(jfs);
        colDatas.add(dfs);

        colDatas.add(jlj);
        colDatas.add(dlj);
        colDatas.add(yefx3);
        colDatas.add(ye);
        //colDatas.add(id);

        colDatas2.add(yefx21);
        colDatas2.add(ncye2);
        colDatas2.add(yefx22);
        colDatas2.add(qcye2);
        colDatas2.add(jfs2);
        colDatas2.add(dfs2);
        colDatas2.add(jlj2);
        colDatas2.add(dlj2);
        colDatas2.add(yefx23);
        colDatas2.add(ye2);
        data.setColData(colDatas);
        data.setColData2(colDatas2);
        return Result.succeed(data, "成功");
    }


    /**
     * 辅助帐余额表表头
     */
    @RequestMapping("/fzxyeb_header")
    public Result<GridHeadDataDto> fzxyebHeader(@RequestParam Map<String, Object> params) {
        boolean kjkmEnable = params.get("kjkmEnable").toString().equals("1");
        boolean ajEnable = params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.get("bmEnable").toString().equals("1");
        TreeMap<Integer, GridHeadDataDto.ColData> hsColMap = new TreeMap<>();
        TreeMap<Integer, GridHeadDataDto.ColData2> hsColMap2 = new TreeMap<>();

        int kjkmXh = StringUtil.toInt(params.get("kjkmXh").toString());
        int ajXh = StringUtil.toInt(params.get("ajXh").toString());
        int pjhXh = StringUtil.toInt(params.get("pjhXh").toString());
        int zphXh = StringUtil.toInt(params.get("zphXh").toString());
        int wldxXh = StringUtil.toInt(params.get("wldxXh").toString());
        int xmXh = StringUtil.toInt(params.get("xmXh").toString());
        int bmXh = StringUtil.toInt(params.get("bmXh").toString());

        GridHeadDataDto data = new GridHeadDataDto();

        List<GridHeadDataDto.ColData> colDatas = new ArrayList<>();
        List<GridHeadDataDto.ColData2> colDatas2 = new ArrayList<>();

        GridHeadDataDto.ColData km = new GridHeadDataDto.ColData.Builder().label("会计科目").name("kjkm").align("left").width(200).build();
        GridHeadDataDto.ColData aj = new GridHeadDataDto.ColData.Builder().label("案件").name("aj").align("left").width(200).build();
        GridHeadDataDto.ColData pjh = new GridHeadDataDto.ColData.Builder().label("票据号").name("pjh").align("left").width(200).build();
        GridHeadDataDto.ColData zph = new GridHeadDataDto.ColData.Builder().label("支票号").name("zph").align("left").width(200).build();
        GridHeadDataDto.ColData wldx = new GridHeadDataDto.ColData.Builder().label("往来对象").name("wldx").align("left").width(200).build();
        GridHeadDataDto.ColData xm = new GridHeadDataDto.ColData.Builder().label("项目").name("xm").align("left").width(200).build();
        GridHeadDataDto.ColData bm = new GridHeadDataDto.ColData.Builder().label("部门").name("bm").align("left").width(200).build();

        GridHeadDataDto.ColData yefx1 = new GridHeadDataDto.ColData.Builder().label("余额方向").name("yefx1Str").align("center").width(80).build();
        GridHeadDataDto.ColData ncye = new GridHeadDataDto.ColData.Builder().label("年初余额").name("ncye").align("right").width(100).formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();
        GridHeadDataDto.ColData yefx2 = new GridHeadDataDto.ColData.Builder().label("余额方向").name("yefx2Str").align("center").width(80).build();
        GridHeadDataDto.ColData qcye = new GridHeadDataDto.ColData.Builder().label("期初余额").name("qcye").align("right").width(100).formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();


        GridHeadDataDto.ColData jfs = new GridHeadDataDto.ColData.Builder().label("借发生").name("jfs").align("right").formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();
        GridHeadDataDto.ColData dfs = new GridHeadDataDto.ColData.Builder().label("贷发生").name("dfs").align("right").formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();
        GridHeadDataDto.ColData fs_group = new GridHeadDataDto.ColData.Builder().label("发生额").name("发生额").align("center").width(200).children(new GridHeadDataDto.ColData[]{jfs, dfs}).build();

        GridHeadDataDto.ColData jlj = new GridHeadDataDto.ColData.Builder().label("借累计").name("jlj").align("right").formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();
        GridHeadDataDto.ColData dlj = new GridHeadDataDto.ColData.Builder().label("贷累计").name("dlj").align("right").formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();
        GridHeadDataDto.ColData lj_group = new GridHeadDataDto.ColData.Builder().label("累计额").name("累计额").align("center").width(200).children(new GridHeadDataDto.ColData[]{jlj, dlj}).build();

        GridHeadDataDto.ColData yefx3 = new GridHeadDataDto.ColData.Builder().label("余额方向").name("yefx3Str").align("center").width(80).build();
        GridHeadDataDto.ColData ye = new GridHeadDataDto.ColData.Builder().label("余额").name("ye").align("right").width(100).formatter("function (cellvalue) { return $.formatMoney(cellvalue, 2)}").build();

        GridHeadDataDto.ColData id = new GridHeadDataDto.ColData.Builder().label("id").name("id").align("center").width(1).hidden(true).build();


        GridHeadDataDto.ColData2 km2 = new GridHeadDataDto.ColData2.Builder().title("会计科目").field("kjkm").colspan(1).rowspan(1).checked(true).columnId("kjkm").width(84).align("left").build();
        GridHeadDataDto.ColData2 aj2 = new GridHeadDataDto.ColData2.Builder().title("案件").field("aj").colspan(1).rowspan(1).checked(true).columnId("aj").width(84).align("left").build();
        GridHeadDataDto.ColData2 pjh2 = new GridHeadDataDto.ColData2.Builder().title("票据号").field("pjh").colspan(1).rowspan(1).checked(true).columnId("pjh").width(84).align("left").build();
        GridHeadDataDto.ColData2 zph2 = new GridHeadDataDto.ColData2.Builder().title("支票号").field("zph").colspan(1).rowspan(1).checked(true).columnId("zph").width(84).align("left").build();
        GridHeadDataDto.ColData2 wldx2 = new GridHeadDataDto.ColData2.Builder().title("往来对象").field("wldx").colspan(1).rowspan(1).checked(true).columnId("wldx").width(84).align("left").build();
        GridHeadDataDto.ColData2 xm2 = new GridHeadDataDto.ColData2.Builder().title("项目").field("xm").colspan(1).rowspan(1).checked(true).columnId("xm").width(84).align("left").build();
        GridHeadDataDto.ColData2 bm2 = new GridHeadDataDto.ColData2.Builder().title("部门").field("bm").colspan(1).rowspan(1).checked(true).columnId("bm").width(84).align("left").build();

        GridHeadDataDto.ColData2 yefx21 = new GridHeadDataDto.ColData2.Builder().title("余额方向").field("yefx1Str").colspan(1).rowspan(1).checked(true).columnId("yefx1Str").width(25).align("center").build();
        GridHeadDataDto.ColData2 ncye2 = new GridHeadDataDto.ColData2.Builder().title("年初余额").field("ncye").colspan(1).rowspan(1).checked(true).columnId("ncye").width(46).align("right").build();
        GridHeadDataDto.ColData2 yefx22 = new GridHeadDataDto.ColData2.Builder().title("余额方向").field("yefx2Str").colspan(1).rowspan(1).checked(true).columnId("yefx2Str").width(25).align("center").build();
        GridHeadDataDto.ColData2 qcye2 = new GridHeadDataDto.ColData2.Builder().title("期初余额").field("qcye").colspan(1).rowspan(1).checked(true).columnId("qcye").width(46).align("right").build();
        GridHeadDataDto.ColData2 jfs2 = new GridHeadDataDto.ColData2.Builder().title("借发生").field("jfs").colspan(1).rowspan(1).checked(true).columnId("jfs").width(46).align("right").build();
        GridHeadDataDto.ColData2 dfs2 = new GridHeadDataDto.ColData2.Builder().title("贷发生").field("dfs").colspan(1).rowspan(1).checked(true).columnId("dfs").width(46).align("right").build();
        GridHeadDataDto.ColData2 jlj2 = new GridHeadDataDto.ColData2.Builder().title("借累计").field("jlj").colspan(1).rowspan(1).checked(true).columnId("jlj").width(46).align("right").build();
        GridHeadDataDto.ColData2 dlj2 = new GridHeadDataDto.ColData2.Builder().title("贷累计").field("dlj").colspan(1).rowspan(1).checked(true).columnId("dlj").width(46).align("right").build();
        GridHeadDataDto.ColData2 yefx23 = new GridHeadDataDto.ColData2.Builder().title("余额方向").field("yefx3Str").colspan(1).rowspan(1).checked(true).columnId("yefx3Str").width(25).align("center").build();
        GridHeadDataDto.ColData2 ye2 = new GridHeadDataDto.ColData2.Builder().title("余额").field("ye").colspan(1).rowspan(1).checked(true).columnId("ye").width(46).align("right").build();


        if (kjkmEnable) {
            hsColMap.put(kjkmXh, km);
            hsColMap2.put(kjkmXh, km2);
        }
        if (ajEnable) {
            hsColMap.put(ajXh, aj);
            hsColMap2.put(ajXh, aj2);
        }
        if (pjhEnable) {
            hsColMap.put(pjhXh, pjh);
            hsColMap2.put(pjhXh, pjh2);
        }
        if (zphEnable) {
            hsColMap.put(zphXh, zph);
            hsColMap2.put(zphXh, zph2);
        }
        if (wldxEnable) {
            hsColMap.put(wldxXh, wldx);
            hsColMap2.put(wldxXh, wldx2);
        }
        if (bmEnable) {
            hsColMap.put(bmXh, bm);
            hsColMap2.put(bmXh, bm2);
        }
        if (xmEnable) {
            hsColMap.put(xmXh, xm);
            hsColMap2.put(xmXh, xm2);
        }
        //保证有序
        for (Map.Entry<Integer, GridHeadDataDto.ColData> m : hsColMap.entrySet()) {
            GridHeadDataDto.ColData val = m.getValue();
            colDatas.add(val);
        }
        for (Map.Entry<Integer, GridHeadDataDto.ColData2> m : hsColMap2.entrySet()) {
            GridHeadDataDto.ColData2 val = m.getValue();
            colDatas2.add(val);
        }

        colDatas.add(yefx1);
        colDatas.add(ncye);
        colDatas.add(yefx2);
        colDatas.add(qcye);
        colDatas.add(fs_group);
//        colDatas.add(jfs);
//        colDatas.add(dfs);
        colDatas.add(lj_group);
//        colDatas.add(jlj);
//        colDatas.add(dlj);
        colDatas.add(yefx3);
        colDatas.add(ye);
        colDatas.add(id);


        colDatas2.add(yefx21);
        colDatas2.add(ncye2);
        colDatas2.add(yefx22);
        colDatas2.add(qcye2);
        colDatas2.add(jfs2);
        colDatas2.add(dfs2);
        colDatas2.add(jlj2);
        colDatas2.add(dlj2);
        colDatas2.add(yefx23);
        colDatas2.add(ye2);
        data.setColData(colDatas);
        data.setColData2(colDatas2);
        return Result.succeed(data, "成功");
    }


    /**
     * 科目三栏账
     */
    @RequestMapping("/slmxz")
    public Result<List<SlmxzDto>> slmxz(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge) {
        List<SlmxzDto> data;
        params = filterParam(params);
        try {
            data = reportService.slmxz(params, merge);
        } catch (Exception e) {
            log.error("/slmxz失败{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data, "成功");
    }


    /**
     * 科目三栏账(导出excel)
     *
     * @return
     */
    @GetMapping("/slmxz/excel")
    public void exportSlmxzExcel(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SlmxzDto> data = new ArrayList<>();
        params = filterParam(params);
        try {
            data = reportService.slmxz(params, merge);
        } catch (Exception e) {
            log.error("/slmxz失败:{}", e);
        }
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
        String kjkmCode = params.get("kjkmCode").toString();
//        BaseKjkm kjkm = kjkmService.findByCodeCache(companyId, ztId, kjnd, kjkmCode); TODO 获取名称
        String kjkmName = kjkmCode;

        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();


        ExcelSlzmxz excel = new ExcelSlzmxz();

        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        int colWidths[] = {100, 100, 260, 150, 150, 80, 150};

        InputStream input = (excel.getExcelFile2(kjkmName, ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, "三栏账", headNames, colWidths));

        String filePath = fileuploadPath + "/temp/excel/slmxz.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "slmxz.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目三栏账(导出pdf)
     *
     * @return
     */
    @GetMapping("/slmxz/pdf")
    public void exportSlmxzPdf(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SlmxzDto> data = new ArrayList<>();
        params = filterParam(params);
        try {
            data = reportService.slmxz(params, merge);
        } catch (Exception e) {
            log.error("/slmxz失败:{}", e);
        }
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
        String kjkmCode = params.get("kjkmCode").toString();
//        BaseKjkm kjkm = kjkmService.findByCodeCache(companyId, ztId, kjnd, kjkmCode); TODO 获取名称
        String kjkmName = kjkmCode;

        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        float colWidths[] = {100, 80, 260, 150, 150, 60, 150};


        String filePath = fileuploadPath + "/temp/pdf/slmxz.pdf";
        Document document = new Document(PageSize.A4);// 建立一个Document对象
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("三栏账账本");// 标题
            new PdfSlmxz().generatePDF(document, kjkmName, ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, headNames, colWidths);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "slmxz.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 辅助项三栏账
     */
    @RequestMapping("/fzxmxz")
    public Result<List<SlmxzDto>> fzxmxz(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge) {
        List<SlmxzDto> data;
        params = filterParam(params);
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        try {
            data = reportService.fzxmxz(params, merge);
        } catch (Exception e) {
            log.error("fzxmxz失败:{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data, "成功");
    }


    /**
     * 辅助项三栏账(导出excel)
     *
     * @return
     */
    @GetMapping("/fzxmxz/excel")
    public void exportFzxmxzExcel(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SlmxzDto> data = new ArrayList<>();
        params = filterParam(params);
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        String kmName = params.get("kmName").toString();
        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        try {
            data = reportService.fzxmxz(params, merge);
        } catch (Exception e) {
            log.error("/fzxmxz失败:{}", e);
        }
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
//        String kmName = "";
//        if (params.containsKey("kjkmCode")) {
//            String kjkmCode = params.get("kjkmCode").toString();
//            BaseKjkm kjkm = kjkmService.findByCodeCache(companyId, ztId, kjnd, kjkmCode);
//            kmName = kjkm.getSimpleName();
//        }
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        ExcelSlzmxz excel = new ExcelSlzmxz();

        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        int colWidths[] = {100, 100, 260, 150, 150, 80, 150};

        InputStream input = (excel.getExcelFile2(kmName, ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, "辅助项三栏账", headNames, colWidths));

        String filePath = fileuploadPath + "/temp/excel/fzxmxz.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "fzxmxz.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 辅助项三栏账(导出pdf)
     *
     * @return
     */
    @GetMapping("/fzxmxz/pdf")
    public void exportFzxmxzPdf(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SlmxzDto> data = new ArrayList<>();
        params = filterParam(params);
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        String kmName = params.get("kmName").toString();
        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        try {
            data = reportService.fzxmxz(params, merge);
        } catch (Exception e) {
            log.error("/fzxmxz失败:{}", e);
        }
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        Integer kjnd = Integer.parseInt(params.get("kjnd").toString());
//        String kmName = "";
//        if (params.containsKey("kjkmCode")) {
//            String kjkmCode = params.get("kjkmCode").toString();
//            BaseKjkm kjkm = kjkmService.findByCodeCache(companyId, ztId, kjnd, kjkmCode);
//            kmName = kjkm.getSimpleName();
//        }
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        float colWidths[] = {100, 100, 260, 150, 150, 80, 150};


        String filePath = fileuploadPath + "/temp/pdf/fzxmxz.pdf";
        Document document = new Document(PageSize.A4);// 建立一个Document对象
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("三栏账账本");// 标题
            new PdfSlmxz().generatePDF(document, kmName, ztId + " " + dictService.findNameByValue("ZT", ztId), pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10), data, headNames, colWidths);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "fzxmxz.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 序时账表头
     */
    @RequestMapping("/xsz_header2")
    public Result<GridHeadDataDto2> xszHeader2(@RequestParam Map<String, Object> params) {
        boolean kjkmEnable = true;
        boolean expecoEnable = params.get("expecoEnable").toString().equals("1");
        boolean expfuncEnable = params.get("expfuncEnable").toString().equals("1");
        boolean projectEnable = params.get("projectEnable").toString().equals("1");
        boolean depproEnable = params.get("depproEnable").toString().equals("1");
        boolean departmentEnable = params.get("departmentEnable").toString().equals("1");
        TreeMap<Integer, GridHeadDataDto2.ColData> hsColMap = new TreeMap<>();

        int kjkmXh = 1;
        int expecoXh = StringUtil.toInt(params.get("expecoXh").toString());
        int expfuncXh = StringUtil.toInt(params.get("expfuncXh").toString());
        int projectXh = StringUtil.toInt(params.get("projectXh").toString());
        int depproXh = StringUtil.toInt(params.get("depproXh").toString());
        int departmentXh = StringUtil.toInt(params.get("departmentXh").toString());

        GridHeadDataDto2 data = new GridHeadDataDto2();

        List<GridHeadDataDto2.ColData> colDatas = new ArrayList<>();

        GridHeadDataDto2.ColData pzrq = new GridHeadDataDto2.ColData.Builder().label("会计日期").name("col_pzrq").align("center").width(80).build();
        GridHeadDataDto2.ColData pzh = new GridHeadDataDto2.ColData.Builder().label("凭证号").name("col_pzh").align("center").width(60).build();
        GridHeadDataDto2.ColData flxh = new GridHeadDataDto2.ColData.Builder().label("序号").name("col_flxh").align("center").width(40).build();
        GridHeadDataDto2.ColData zy = new GridHeadDataDto2.ColData.Builder().label("摘要").name("col_zy").align("left").width(240).build();

        GridHeadDataDto2.ColData km = new GridHeadDataDto2.ColData.Builder().label("科目").name("col_kjkm").align("left").width(180).build();
        GridHeadDataDto2.ColData aj = new GridHeadDataDto2.ColData.Builder().label("部门经济分类").name("col_expeco").align("left").width(180).build();
        GridHeadDataDto2.ColData pjh = new GridHeadDataDto2.ColData.Builder().label("支出功能分类").name("col_expfunc").align("left").width(180).build();
        GridHeadDataDto2.ColData zph = new GridHeadDataDto2.ColData.Builder().label("项目").name("col_project").align("left").width(180).build();
        GridHeadDataDto2.ColData wldx = new GridHeadDataDto2.ColData.Builder().label("预算项目").name("col_deppro").align("left").width(180).build();
        GridHeadDataDto2.ColData bm = new GridHeadDataDto2.ColData.Builder().label("部门").name("col_department").align("left").width(180).build();

        GridHeadDataDto2.ColData jfs = new GridHeadDataDto2.ColData.Builder().label("借方额").name("col_j").align("right").width(100).build();
        GridHeadDataDto2.ColData dfs = new GridHeadDataDto2.ColData.Builder().label("贷方额").name("col_d").align("right").width(100).build();
        GridHeadDataDto2.ColData yefx = new GridHeadDataDto2.ColData.Builder().label("借/贷").name("col_yefxStr").align("center").width(40).build();
        GridHeadDataDto2.ColData ye = new GridHeadDataDto2.ColData.Builder().label("余额").name("col_ye").align("right").width(100).build();
        GridHeadDataDto2.ColData id = new GridHeadDataDto2.ColData.Builder().label("id").name("col_pzid").align("center").width(1).hidden(true).build();

        if (kjkmEnable) {
            hsColMap.put(kjkmXh, km);
        }
        if (expecoEnable) {
            hsColMap.put(expecoXh, aj);
        }
        if (expfuncEnable) {
            hsColMap.put(expfuncXh, pjh);
        }
        if (projectEnable) {
            hsColMap.put(projectXh, zph);
        }
        if (depproEnable) {
            hsColMap.put(depproXh, wldx);
        }
        if (departmentEnable) {
            hsColMap.put(departmentXh, bm);
        }

        colDatas.add(pzrq);
        colDatas.add(pzh);
        colDatas.add(flxh);
        colDatas.add(zy);
        //保证有序
        for (Map.Entry<Integer, GridHeadDataDto2.ColData> m : hsColMap.entrySet()) {
            GridHeadDataDto2.ColData val = m.getValue();
            colDatas.add(val);
        }
        colDatas.add(jfs);
        colDatas.add(dfs);
        colDatas.add(yefx);
        colDatas.add(ye);
        colDatas.add(id);

        data.setColData(colDatas);
        return Result.succeed(data, "成功");
    }


    /**
     * 序时账
     */
    @RequestMapping("/xsz")
    public Result<List<XszDto>> xsz(@RequestParam Map<String, Object> params) {
        List<XszDto> data;
        params = filterParam(params);
        boolean expecoEnable = params.containsKey("expecoEnable") && params.get("expecoEnable").toString().equals("1");
        boolean expfuncEnable = params.containsKey("expfuncEnable") && params.get("expfuncEnable").toString().equals("1");
        boolean projectEnable = params.containsKey("projectEnable") && params.get("projectEnable").toString().equals("1");
        boolean depproEnable = params.containsKey("depproEnable") && params.get("depproEnable").toString().equals("1");
        boolean departmentEnable = params.containsKey("departmentEnable") && params.get("departmentEnable").toString().equals("1");
        if (!expecoEnable) {
            params.remove("expecoCodes");
            params.remove("expecoCode");
        }
        if (!expfuncEnable) {
            params.remove("expfuncCodes");
            params.remove("expfuncCode");
        }
        if (!projectEnable) {
            params.remove("projectCodes");
            params.remove("projectCode");
        }
        if (!depproEnable) {
            params.remove("depproCodes");
            params.remove("depproCode");
        }
        if (!departmentEnable) {
            params.remove("departmentCodes");
            params.remove("departmentCode");
        }

        try {
            data = reportService.xsz(params);
        } catch (Exception e) {
            log.error("xsz失败:{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data, "成功");
    }


    /**
     * 序时账(导出excel)
     *
     * @return
     */
    @GetMapping("/xsz/excel")
    public void exportXszExcel(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<XszDto> data = new ArrayList<>();
        params = filterParam(params);
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        boolean judgeEnable = params.containsKey("judgeEnable") && params.get("judgeEnable").toString().equals("1");
        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        if (!judgeEnable) {
            params.remove("ajJudge");
        }

        try {
            data = reportService.xsz(params);
        } catch (Exception e) {
            log.error("/xsz失败:{}", e);
        }
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        ExcelXsz excel = new ExcelXsz();
        List<String> heads1 = new ArrayList<>();
        List<Integer> heads2 = new ArrayList<>();
        heads1.add("会计期间");
        heads2.add(100);
        heads1.add("凭证号");
        heads2.add(100);
        heads1.add("序号");
        heads2.add(50);
        heads1.add("摘要");
        heads2.add(260);
        heads1.add("会计科目");
        heads2.add(260);
        if (ajEnable) {
            heads1.add("案件");
            heads2.add(220);
        }
        if (pjhEnable) {
            heads1.add("票据号");
            heads2.add(220);
        }
        if (zphEnable) {
            heads1.add("支票号");
            heads2.add(220);
        }
        if (wldxEnable) {
            heads1.add("往来单位");
            heads2.add(220);
        }
        if (xmEnable) {
            heads1.add("项目");
            heads2.add(220);
        }
        if (bmEnable) {
            heads1.add("部门");
            heads2.add(220);
        }
        if (judgeEnable) {
            heads1.add("经办法官");
            heads2.add(100);
        }
        heads1.add("借方");
        heads2.add(150);
        heads1.add("贷方");
        heads2.add(150);
        heads1.add("借贷");
        heads2.add(80);
        heads1.add("余额");
        heads2.add(150);
        String[] headNames = heads1.toArray(new String[heads1.size()]);
        int colWidths[] = new int[heads2.size()];
        for (int i = 0; i < heads2.size(); i++) {
            colWidths[i] = heads2.get(i);
        }

        InputStream input = (excel.getExcelFile(ztId + " " + dictService.findNameByValue("ZT", ztId), (StringUtil.isEmpty(pzrqStart) ? "" : pzrqStart.substring(0, 10)) + "至" + pzrqEnd.substring(0, 10), data, "三栏账", headNames, colWidths));

        String filePath = fileuploadPath + "/temp/excel/xsz.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "xsz.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 序时账(导出pdf)
     *
     * @return
     */
    @GetMapping("/xsz/pdf")
    public void exportXszPdf(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<XszDto> data = new ArrayList<>();
        params = filterParam(params);
        boolean ajEnable = params.containsKey("ajEnable") && params.get("ajEnable").toString().equals("1");
        boolean pjhEnable = params.containsKey("pjhEnable") && params.get("pjhEnable").toString().equals("1");
        boolean zphEnable = params.containsKey("zphEnable") && params.get("zphEnable").toString().equals("1");
        boolean wldxEnable = params.containsKey("wldxEnable") && params.get("wldxEnable").toString().equals("1");
        boolean xmEnable = params.containsKey("xmEnable") && params.get("xmEnable").toString().equals("1");
        boolean bmEnable = params.containsKey("bmEnable") && params.get("bmEnable").toString().equals("1");
        boolean judgeEnable = params.containsKey("judgeEnable") && params.get("judgeEnable").toString().equals("1");
        if (!ajEnable) {
            params.remove("ajCodes");
            params.remove("ajCode");
        }
        if (!pjhEnable) {
            params.remove("pjhCodes");
            params.remove("pjhCode");
        }
        if (!zphEnable) {
            params.remove("zphCodes");
            params.remove("zphCode");
        }
        if (!wldxEnable) {
            params.remove("wldxCodes");
            params.remove("wldxCode");
        }
        if (!xmEnable) {
            params.remove("xmCodes");
            params.remove("xmCode");
        }
        if (!bmEnable) {
            params.remove("bmCodes");
            params.remove("bmCode");
        }
        if (!judgeEnable) {
            params.remove("ajJudge");
        }

        try {
            data = reportService.xsz(params);
        } catch (Exception e) {
            log.error("/xsz失败:{}", e);
        }
        String companyId = params.get("companyId").toString();
        String ztId = params.get("ztId").toString();
        String pzrqStart = params.get("pzrqStart").toString();
        String pzrqEnd = params.get("pzrqEnd").toString();

        List<String> heads1 = new ArrayList<>();
        List<Integer> heads2 = new ArrayList<>();
        heads1.add("会计期间");
        heads2.add(120);
        heads1.add("凭证号");
        heads2.add(70);
        heads1.add("序号");
        heads2.add(50);
        heads1.add("摘要");
        heads2.add(250);
        heads1.add("会计科目");
        heads2.add(220);
        if (ajEnable) {
            heads1.add("案件");
            heads2.add(220);
        }
        if (pjhEnable) {
            heads1.add("票据号");
            heads2.add(220);
        }
        if (zphEnable) {
            heads1.add("支票号");
            heads2.add(220);
        }
        if (wldxEnable) {
            heads1.add("往来单位");
            heads2.add(220);
        }
        if (xmEnable) {
            heads1.add("项目");
            heads2.add(220);
        }
        if (bmEnable) {
            heads1.add("部门");
            heads2.add(220);
        }
        if (judgeEnable) {
            heads1.add("经办法官");
            heads2.add(100);
        }
        heads1.add("借方");
        heads2.add(150);
        heads1.add("贷方");
        heads2.add(150);
        heads1.add("借贷");
        heads2.add(60);
        heads1.add("余额");
        heads2.add(150);
        String[] headNames = heads1.toArray(new String[heads1.size()]);
        float colWidths[] = new float[heads2.size()];
        for (int i = 0; i < heads2.size(); i++) {
            colWidths[i] = heads2.get(i);
        }

        String filePath = fileuploadPath + "/temp/pdf/xsz.pdf";
        Document document = new Document(PageSize.A4);// 建立一个Document对象
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("序时账");// 标题
            new PdfXsz().generatePDF(document, ztId + " " + dictService.findNameByValue("ZT", ztId), (StringUtil.isEmpty(pzrqStart) ? "" : pzrqStart.substring(0, 10)) + "至" + pzrqEnd.substring(0, 10), data, headNames, colWidths);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "xsz.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目三栏账账本打印(目录)
     */
    @RequestMapping("/slmxzbookIndex")
    public Result<List<ZbIndexDto>> slmxzsIndex(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge) {
        ZbBookDto data;
        params = filterParam(params);
        try {
            data = reportService.slmxzBook(params, merge);
        } catch (Exception e) {
            log.error("/slmxzbookIndex:{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data.index, "成功");
    }

    /**
     * 科目三栏账账本打印
     */
    @RequestMapping("/slmxzbook")
    public Result<ZbBookDto> slmxzbook(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge) {
        ZbBookDto data;
        params = filterParam(params);
        try {
            data = reportService.slmxzBook(params, merge);
        } catch (Exception e) {
            log.error("/slmxzbook:{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data, "成功");
    }

    /**
     * 科目三栏账账本 (导出excel)
     */
    @GetMapping("/slmxzbook/excel")
    public void exportSlmxzbookExcel(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ZbBookDto result = new ZbBookDto();
        params = filterParam(params);
        try {
            result = reportService.slmxzBook(params, false);
        } catch (Exception e) {
            log.error("/slmxzbook:{}", e);
        }

        ExcelSlzzb excel = new ExcelSlzzb();

        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        int colWidths[] = {100, 100, 260, 150, 150, 80, 150};

        InputStream input = (excel.getExcelFile(result, "三栏账", headNames, colWidths));

        String filePath = fileuploadPath + "/temp/excel/slzzb.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "slzzb.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目三栏账账本 (导出pdf)
     */
    @GetMapping("/slmxzbook/pdf")
    public void exportSlmxzbookPdf(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ZbBookDto result = new ZbBookDto();
        params = filterParam(params);
        try {
            result = reportService.slmxzBook(params, false);
        } catch (Exception e) {
            log.error("/slmxzbook:{}", e);
        }
        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        float colWidths[] = {108, 78, 260, 150, 150, 60, 150};

        String filePath = fileuploadPath + "/temp/pdf/slzzb.pdf";
        Document document = new Document(PageSize.A4);// 建立一个Document对象
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            //writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("三栏账账本");// 标题
            new PdfSlzzb().generatePDF(document, result, headNames, colWidths, false);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "slzzb.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目三栏账账本 (导出无目录pdf)
     */
    @GetMapping("/slmxzbook/pdf2")
    public void exportSlmxzbookPdf2(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ZbBookDto result = new ZbBookDto();
        params = filterParam(params);
        try {
            result = reportService.slmxzBook(params, false);
        } catch (Exception e) {
            log.error("/slmxzbook:{}", e);
        }
        String[] headNames = {"会计日期", "凭证号", "摘要", "借方", "贷方", "借贷", "期末余额"};
        float colWidths[] = {108, 78, 260, 150, 150, 60, 150};

        String filePath = fileuploadPath + "/temp/pdf/slzzb.pdf";
        Document document = new Document(PageSize.A4);// 建立一个Document对象
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            //writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("三栏账账本");// 标题
            new PdfSlzzb().generatePDF(document, result, headNames, colWidths, true);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "slzzb.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目总账账本打印(目录)
     */
    @RequestMapping("/kmzzbookIndex")
    public Result<List<ZbIndexDto>> kmzzbookIndex(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge) {
        ZbBookDto data;
        filterParam(params);
        try {
            data = reportService.kmzzBook(params, merge);
        } catch (Exception e) {
            log.error("/kmzzbookIndex:{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data.index, "成功");
    }

    /**
     * 科目总账账本打印
     */
    @RequestMapping("/kmzzbook")
    public Result<ZbBookDto> kmzzbook(@RequestParam Map<String, Object> params, @RequestParam(value = "merge", defaultValue = "0") boolean merge) {
        ZbBookDto data;
        filterParam(params);
        try {
            data = reportService.kmzzBook(params, merge);
        } catch (Exception e) {
            log.error("/kmzzbook:{}", e);
            return Result.failed(e.getMessage());
        }
        return Result.succeed(data, "成功");
    }

    /**
     * 科目总账账本打印 (导出excel)
     */
    @GetMapping("/kmzzbook/excel")
    public void exportKmzzbookExcel(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ZbBookDto result = new ZbBookDto();
        filterParam(params);
        try {
            result = reportService.kmzzBook(params, false);
        } catch (Exception e) {
            log.error("/kmzzbook:{}", e);
        }

        ExcelZzzb excel = new ExcelZzzb();

        String[] headNames = {"月份", "摘要", "借方", "贷方", "借贷", "余额"};
        int colWidths[] = {80, 260, 150, 150, 80, 150};

        InputStream input = (excel.getExcelFile(result, "总分类账", headNames, colWidths));

        String filePath = fileuploadPath + "/temp/excel/zzzb.xls";
        File f = new File(filePath);
        if (f.exists())
            f.delete();
        f.createNewFile();
        FileOutputStream out = new FileOutputStream(f);
        HSSFWorkbook book = new HSSFWorkbook(input);
        book.write(out);
        out.flush();
        out.close();

        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "zzzb.xls"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }

    /**
     * 科目总账账本打印 (导出pdf)
     */
    @GetMapping("/kmzzbook/pdf")
    public void exportKmzzbookPdf(@RequestParam Map<String, Object> params, ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ZbBookDto result = new ZbBookDto();
        filterParam(params);
        try {
            result = reportService.kmzzBook(params, false);
        } catch (Exception e) {
            log.error("/kmzzbook:{}", e);
        }

        String[] headNames = {"月份", "摘要", "借方", "贷方", "借贷", "余额"};
        float colWidths[] = {80, 260, 150, 150, 80, 150};

        String filePath = fileuploadPath + "/temp/pdf/zzzb.pdf";
        Document document = new Document(PageSize.A4);// 建立一个Document对象
        try {
            File file = new File(filePath);
            if (file.exists())
                file.delete();
            file.createNewFile();
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
            writer.setPageEvent(new PdfWatermark("禁止外泄"));// 水印
            //writer.setPageEvent(new PdfHeaderFooter());// 页眉/页脚
            document.open();
            document.addTitle("总账账本");// 标题
            new PdfZzzb().generatePDF(document, result, headNames, colWidths);
        } catch (Exception e) {
            log.error(e.toString());
        } finally {
            document.close();
        }
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("multipart/form-data");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, "zzzb.pdf"));
            FileUtils.writeBytes(filePath, response.getOutputStream());
            if (false) {
                FileUtils.deleteFile(filePath);
            }
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }
}
